package com.wyl.xue.admin.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.xue.admin.system.mybatis.entity.SystemUsers;
import com.wyl.xue.admin.system.service.Department;
import com.wyl.xue.admin.system.service.Role;
import com.wyl.xue.admin.system.service.User;
import com.wyl.xue.admin.system.vo.DepartmentTree;
import com.wyl.xue.admin.system.vo.RoleInfoVo;
import com.wyl.xue.admin.system.vo.UserInfoVo;
import com.wyl.xue.admin.system.vo.UserLoginInfo;
import com.wyl.xue.core.util.result.WebResponse;
import com.wyl.xue.core.util.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;


/**
 * @ClassName: UserController
 * @Function: 用户接口
 * @Date: 2019/12/22 17:26
 * @author wyl
 * @version V1.0
 */
@RequestMapping("/admin/v1/user")
@Api(tags = {"用户信息接口"})
@AllArgsConstructor
@RestController
@CrossOrigin
public class UserController {
    final User user;
    final Department department;
    final Role role;

    @ApiOperation(value = "用户登录接口", notes = "返回的token需要填写到相关请求头中")
    @PostMapping(value = "/login")
    public WebResult<String> login(@RequestBody UserLoginInfo userLoginInfo) {
        return WebResponse.WebResponse.ok(user.login(userLoginInfo));
    }

    @ApiOperation(value = "用户登出")
    @PostMapping(value = "/logout")
    public WebResult<String> logout() {
        return WebResponse.WebResponse.ok();
    }


    @ApiOperation(value = "用户新增接口", notes = "新建一个用户")
    @PostMapping(value = "/")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public WebResult<Boolean> addUser(@Valid @RequestBody UserInfoVo userInfo) {
        user.addUser(userInfo);
        return WebResponse.WebResponse.ok();
    }

    @ApiOperation(value = "用户更新接口", notes = "更新用户的信息")
    @PutMapping(value = "/{userId}")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public WebResult<Boolean> changeUser(@PathVariable Long userId, @RequestBody SystemUsers userInfo) {
        user.updateUser(userInfo);
        return WebResponse.WebResponse.ok();
    }

    @ApiOperation(value = "用户删除接口", notes = "根据用户ID删除用户")
    @DeleteMapping(value = "/{userId}")
    @PreAuthorize("hasAuthority('sys:user:del')")
    public WebResult<Boolean> deleteUser(@PathVariable Long userId) {
        user.deleteUser(userId);
        return WebResponse.WebResponse.ok();
    }


    @ApiOperation(value = "获取部门树", notes = "获取部门树")
    @GetMapping(value = "/tree")
    @PreAuthorize("hasAuthority('sys:user:get')")
    public WebResult<List<DepartmentTree>> getDepartmentTree() {
        return WebResponse.WebResponse.ok(department.getDepartmentTree(-1L));
    }

    @ApiOperation(value = "根据部门ID获取用户列表", notes = "根据部门ID获取用户列表")
    @GetMapping(value = "/{departmentId}/{page}/{size}")
    @PreAuthorize("hasAuthority('sys:user:get')")
    public WebResult<IPage<SystemUsers>> getUserInfoListByDepartment(@PathVariable Long departmentId, @PathVariable Integer page, @PathVariable Integer size) {
        return WebResponse.WebResponse.ok(user.getUserInfoBydepartmentId(departmentId, page, size));
    }

    @ApiOperation(value = "设置用户角色")
    @PutMapping(value = "/{userId}/roles")
    @PreAuthorize("hasAuthority('sys:user:setRole')")
    public WebResult<Boolean> setUserRoles(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        user.setUserRoles(userId, roleIds);
        return WebResponse.WebResponse.ok();
    }


    @ApiOperation(value = "获取用户角色")
    @GetMapping(value = "/{userId}/roles")
    @PreAuthorize("hasAuthority('sys:user:setRole')")
    public WebResult<List<Long>> getUserRoles(@PathVariable Long userId) {
        return WebResponse.WebResponse.ok(role.getRoleIdsByUserId(userId));
    }

    @GetMapping(value = "/roles")
    @ApiOperation(value = "获取所有角色信息")
    @PreAuthorize("hasAuthority('sys:user:setRole')")
    public WebResult<List<RoleInfoVo>> getRolesInfo() {
        return WebResponse.WebResponse.ok(role.getRoleAll());
    }


    @ApiOperation(value = "查询用户信息", notes = "获取当前用户信息")
    @GetMapping(value = "/")
    public WebResult<UserInfoVo> getUserInfo() {
        return WebResponse.WebResponse.ok(user.getCurrentUserInfo());
    }

    @ApiOperation(value = "获取用户可以访问的路由")
    @GetMapping(value = "/router")
    public WebResult<Set<Long>> getUserRouter() {
        return WebResponse.WebResponse.ok(user.getCurrentUserAccessRouter());
    }

    @PostMapping(value = "/search")
    @ApiOperation(value = "获取菜单树 不包含按钮")
    @PreAuthorize("hasAuthority('sys:user:search')")
    public WebResult<List<SystemUsers>> search(@RequestBody UserInfoVo userInfoVo) {
        return WebResponse.WebResponse.ok(user.searchUser(userInfoVo));
    }

}
